package com.beerq.view.purchase;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.beerq.R;
import com.beerq.util.AppConfig;
import com.beerq.util.BasicHttp;
import com.beerq.util.Constants;
import com.beerq.view.BaseActivity;
import com.beerq.view_manager.AddImageManager;
import com.beerq.view_manager.CommonManager;
import com.beerq.view_manager.EvaluateManager;
import com.bumptech.glide.Glide;

public class PostEvaluateActivity extends BaseActivity {

    private LinearLayout lin_good_evalute;
    private LinearLayout lin_normal_evalute;
    private LinearLayout lin_bad_evalute;

    private CheckBox mChNoName;//匿名评价

    //评价 1是好评 2是中评 3是差评 默认是好评
    private int mSelectEvaluteValue=1;

    AddImageManager mAddImageManager;

    int mProductId=-1;

    private String base64String="";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        statusBarType=1;
        super.onCreate(savedInstanceState);
        setMainContentView(R.layout.activity_post_evaluate);
        setBarTitle("编辑评价");
        setRightTitle("关闭");

        mChNoName=(CheckBox)findViewById(R.id.ch_no_name);

        mProductId=getIntent().getIntExtra("itemId",-1);

        Glide.with(mContext).load(getIntent().getStringExtra("imgUrl")).into((ImageView)findViewById(R.id.img_product_icon));

        initView();

        mAddImageManager=new AddImageManager(this,1);
        mAddImageManager.getImages().clear();
//        mAddImageManager.imageCache.clear();
        mAddImageManager.bind();

    }

    @Override
    protected void onResume() {
        super.onResume();

        mAddImageManager.refresh();

    }

    private void initView(){
        lin_good_evalute=(LinearLayout)findViewById(R.id.lin_good_evalute);
        lin_bad_evalute=(LinearLayout)findViewById(R.id.lin_bad_evalute);
        lin_normal_evalute=(LinearLayout)findViewById(R.id.lin_normal_evalute);

        lin_good_evalute.setOnClickListener(mOnClickListener);
        lin_bad_evalute.setOnClickListener(mOnClickListener);
        lin_normal_evalute.setOnClickListener(mOnClickListener);

        findViewById(R.id.ch_normal_evalute).setOnClickListener(mChOnClickListener);
        findViewById(R.id.ch_good_evalute).setOnClickListener(mChOnClickListener);
        findViewById(R.id.ch_bad_evalute).setOnClickListener(mChOnClickListener);

        findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mAddImageManager.getImages().size()>0){
                    showLoadingDialog(false);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {

                            String path=mAddImageManager.getImages().get(0).path;
                            base64String= AppConfig.compressImageReturnBase64(path);
//                            base64String=  CommonManager.bitmap2StrByBase64(bitmap,mAddImageManager.getImages().get(0).path);
                            Message message= Message.obtain();
                            message.what=1;
                            mHandler.sendMessage(message);



//                            BitmapDrawable bitmapDrawable=(BitmapDrawable) BitmapDrawable.createFromPath(mAddImageManager.getImages().get(0).path);
//                            base64String=  CommonManager.bitmap2StrByBase64(bitmapDrawable.getBitmap(),mAddImageManager.getImages().get(0).name);
//                            Message message= Message.obtain();
//                            message.what=1;
//                            mHandler.sendMessage(message);

                        }
                    }).start();
                }else{
//                    showToast("图片不能为空");
//                    return;
                    postEvaluate("",mChNoName.isChecked()?-1:Constants.userInfo.getAppUserId());
                }
            }
        });

    }

    Handler mHandler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if(msg.what==0){
                finish();
            }else if(msg.what==1){
                CommonManager.uploadImage(mContext, mAddImageManager.getImages().get(0).name, base64String, new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(String resonseData) {
                        JSONObject jsonObject=JSONObject.parseObject(resonseData);
                        postEvaluate(jsonObject.getJSONObject("Result").getString("FileFullUrl"),mChNoName.isChecked()?-1:Constants.userInfo.getAppUserId());
                    }
                    @Override
                    public void onFailure(String resonseData) {

                    }
                });
            }
            return false;
        }
    });


    private void postEvaluate(String imgUrl,int userName){
        EvaluateManager.postEvaluate(mContext, getIntent().getStringExtra("code"),((EditText) findViewById(R.id.edit_content)).getText().toString(), mSelectEvaluteValue, mProductId, imgUrl, userName, new BasicHttp.IMyCallBack() {
            @Override
            public void onResponse(String resonseData) {
                showToast("评价成功！");
                mHandler.sendEmptyMessageDelayed(0,2000);
            }

            @Override
            public void onFailure(String resonseData) {

            }
        });
    }

    View.OnClickListener mChOnClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            resetView();
            viewSelect((View)v.getParent());
        }
    };



    View.OnClickListener mOnClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            resetView();
            viewSelect(v);
        }
    };

    private void resetView(){
        ((TextView)lin_good_evalute.findViewWithTag("txt")).setTextColor(getResources().getColor(R.color.black));
        ((TextView)lin_normal_evalute.findViewWithTag("txt")).setTextColor(getResources().getColor(R.color.black));
        ((TextView)lin_bad_evalute.findViewWithTag("txt")).setTextColor(getResources().getColor(R.color.black));

        ((Checkable)lin_good_evalute.findViewWithTag("img")).setChecked(false);
        ((Checkable)lin_normal_evalute.findViewWithTag("img")).setChecked(false);
        ((Checkable)lin_bad_evalute.findViewWithTag("img")).setChecked(false);

    }

    private void viewSelect(View view){
        mSelectEvaluteValue=Integer.parseInt(view.getTag().toString());
        ((TextView)view.findViewWithTag("txt")).setTextColor(0xffAA8217);
        ((Checkable)view.findViewWithTag("img")).setChecked(true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mAddImageManager.onActivityResult(requestCode,resultCode,data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();



//        mAddImageManager.clearBitMap();
    }
}
